var primePools = [];
var primes = [];

function getPrimes(limit) {
    var sqrt = Math.floor(Math.sqrt(limit));
    primePools[2] = 0;
    for(var i=2; i<=sqrt; i++) {
        for(var j=i*2; j<=limit; j=j+i) {
            primePools[j] = 1;
        }
    }
}

function isPrime(num) {
    return primePools[num] != 1
}

print('get primes.....');
var t=new Date().getTime();
var plimit = 1000000
plimit=1000000;
getPrimes(plimit);
for(var i=2; i<=plimit; i++) {
    if(isPrime(i)) primes.push(i);
}
print('get primes done..... ' + ((new Date().getTime()-t)/1000) + "s");

function p50(limit) {
    var result = 0, term =0;
    for(var j=0; j<primes.length; j++) {
        var sum=0;
        for(var i=j; i<primes.length; i++) {
            sum +=primes[i];
            if((i-j+1)>term && isPrime(sum) && sum<limit) {
                term = i-j+1;
                result = sum;
                print("f("+term+")=" + sum);
            }
            if(sum>limit) break;
        }
    }
    return result;
}

p50(plimit);
